package com.adinnet.admin.operation.service.impl;

import com.adinnet.admin.common.service.CommonService;
import com.adinnet.admin.operation.model.Operation;
import com.adinnet.admin.operation.mapper.OperationMapper;
import com.adinnet.admin.operation.model.vo.OperationResp;
import com.adinnet.admin.operation.service.OperationService;
import com.adinnet.admin.system.model.LoginRecord;
import com.adinnet.admin.system.model.vo.SysUserInfo;
import com.adinnet.common.biz.enums.RoleCode;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 操作日志 服务实现类
 * </p>
 *
 * @author liukp
 * @since 2020-12-14
 */
@Service
public class OperationServiceImpl extends ServiceImpl<OperationMapper, Operation> implements OperationService {

    @Autowired
    private CommonService commonService;

    @Override
    public PageResult<OperationResp> listFull(Integer page, Integer limit, String startDate, String endDate,String userName, Integer schoolId) {
        if (page == null || limit == null) {
            page = 1;
            limit = 10;
        }
        if (StringUtils.isBlank(startDate)) {
            startDate = null;
        } else {
            startDate += " 00:00:00";
        }
        if (StringUtils.isBlank(endDate)) {
            endDate = null;
        } else {
            endDate += " 23:59:59";
        }

        SysUserInfo sysUserInfo=commonService.getLoginUser();
        Integer organizationId=0;
        Integer schoolSqlId=0;
        if(RoleCode.ORGANIZATION.getCode().equals(sysUserInfo.getMaxRole())){
            organizationId=sysUserInfo.getOrganizationId();
        }
        if(RoleCode.SCHOOL.getCode().equals(sysUserInfo.getMaxRole())){
            schoolSqlId=sysUserInfo.getSchoolId();
        }
        if(schoolId!=null && schoolId.intValue()>0){
            schoolSqlId=schoolId;
        }

        Page<OperationResp> iPage = new Page<>(page, limit);
        List<OperationResp> records = baseMapper.listFull(iPage, startDate, endDate, userName,organizationId,schoolSqlId);
        return new PageResult<>(records, iPage.getTotal());
    }
}
